package com.hy.frame.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;

import com.hy.frame.widget.ui.R;

public class DesignImageHelper extends DesignHelper {

    private final RectF roundRect = new RectF();
    private final RectF borderRect = new RectF();
    private final Paint maskPaint = new Paint();
    private final Paint zonePaint = new Paint();
    private final Paint borderPaint = new Paint();
    private final Paint borderMaskPaint = new Paint();
    private final Path path = new Path();

    protected float radius = 15;
    protected boolean circle;
    protected int border;
    protected int borderColor;
    protected int radiusLeftTop = 0;
    protected int radiusRightTop = 0;
    protected int radiusLeftBottom = 0;
    protected int radiusRightBottom = 0;

    public DesignImageHelper(@NonNull View target) {
        super(target);
    }

    public void loadImageAttributes(AttributeSet attrs, int defStyleAttr) {
        if (mTarget == null || mTarget.get() == null) return;
        View v = mTarget.get();
        Context context = v.getContext();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IDesignImageView, defStyleAttr, 0);
        this.circle = a.getBoolean(R.styleable.IDesignImageView_designCircle, false);
        this.border = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignImageView_designBorder, 0));
        this.borderColor = a.getColor(R.styleable.IDesignImageView_designBorderColor, Color.BLACK);
        this.radius = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignImageView_designRadius, 0));
        this.radiusLeftTop = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignImageView_designRadiusLeftTop, 0));
        this.radiusRightTop = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignImageView_designRadiusRightTop, 0));
        this.radiusLeftBottom = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignImageView_designRadiusLeftBottom, 0));
        this.radiusRightBottom = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignImageView_designRadiusRightBottom, 0));
        a.recycle();
        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);
        borderPaint.setAntiAlias(true);
        borderPaint.setColor(Color.WHITE);
        borderMaskPaint.setAntiAlias(true);
        borderMaskPaint.setColor(borderColor);
        borderMaskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
    }

    public void setCircle(boolean circle) {
        this.circle = circle;
    }

    public void setBorder(int border) {
        this.border = border;
    }

    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
        borderMaskPaint.setColor(borderColor);
    }

    public void setRadiusLeftTop(int radiusLeftTop) {
        this.radiusLeftTop = radiusLeftTop;
    }

    public void setRadiusRightTop(int radiusRightTop) {
        this.radiusRightTop = radiusRightTop;
    }

    public void setRadiusLeftBottom(int radiusLeftBottom) {
        this.radiusLeftBottom = radiusLeftBottom;
    }

    public void setRadiusRightBottom(int radiusRightBottom) {
        this.radiusRightBottom = radiusRightBottom;
    }

    public void draw(Canvas canvas) {
        canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
        if (circle) {
            path.addCircle(roundRect.width() / 2, roundRect.height() / 2, radius - border, Path.Direction.CCW);
            //canvas.drawCircle(roundRect.width() / 2, roundRect.height() / 2, radius - border, zonePaint);
        } else if (radiusLeftTop > 0 || radiusRightTop > 0 || radiusRightBottom > 0 || radiusLeftBottom > 0) {
            float[] radii = {radiusLeftTop, radiusLeftTop,
                    radiusRightTop, radiusRightTop,
                    radiusRightBottom, radiusRightBottom,
                    radiusLeftBottom, radiusLeftBottom};
            path.addRoundRect(borderRect, radii, Path.Direction.CCW);
//            canvas.drawRoundRect(borderRect, radius, radius, zonePaint);
        } else {
            path.addRoundRect(borderRect, radius, radius, Path.Direction.CCW);
//            canvas.drawRoundRect(borderRect, radius, radius, zonePaint);
        }
        canvas.drawPath(path, zonePaint);
        canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
    }

    public void drawBorder(Canvas canvas) {
        canvas.restore();
        //画边框
        if (border > 0) {
            canvas.saveLayer(roundRect, borderPaint, Canvas.ALL_SAVE_FLAG);
            if (circle) {
                canvas.drawCircle(roundRect.width() / 2, roundRect.height() / 2, radius - border, borderPaint);
            } else {
                canvas.drawRoundRect(borderRect, radius, radius, borderPaint);
            }
            canvas.saveLayer(roundRect, borderMaskPaint, Canvas.ALL_SAVE_FLAG);
            if (circle) {
                canvas.drawCircle(roundRect.width() / 2, roundRect.height() / 2, radius, borderMaskPaint);
            } else {

                canvas.drawRoundRect(roundRect, radius, radius, borderMaskPaint);
            }
            canvas.restore();
        }
    }

    @Override
    public void onLayout(int w, int h) {
        super.onLayout(w, h);
        roundRect.set(0, 0, w, h);
        borderRect.set(border, border, w - border, h - border);
        if (circle) {
            if (w > h) {
                radius = h * 1f / 2;
            } else {
                radius = w * 1f / 2;
            }
        }
    }

    /**
     * 设置圆角大小
     *
     * @param radius
     */
    public void setRadius(float radius) {
        this.radius = radius;
    }
}
